package gui.view;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;

import utils.ColumnGroup;
import utils.ComplexColumn;
import utils.SimpleColumn;

/**
 * Class which extends the functionality of DefaultColumnTableModel to
 * also provide capabilities to group columns. This can be used for
 * instance to aid in the layout of groupable table headers.
 * 
 * @author D'Er'Nasherbrezon aka dernasherbrezon %)
 *
 */
public class GroupableTableColumnModel extends  DefaultTableColumnModel
{
    /**
     * Hold the list of ColumnGroups which define what group each normal
     * column is within, if any.
     */    
    public ArrayList<ColumnGroup> columnGroups = new ArrayList<ColumnGroup>();
    
    /**
     * Add a new columngroup.
     * @param columnGroup new ColumnGroup
     */    
    public void addColumnGroup(ColumnGroup columnGroup)
    {
        columnGroups.add(columnGroup);
        for(int i = 0; i<columnGroup.childs.size();i++)
        {
        	if(columnGroup.childs.get(i) instanceof ColumnGroup)
        	{
        		addColumnGroup((ColumnGroup)columnGroup.childs.get(i));
        	}
        }
    }
    
    @Override
    public void addColumn(TableColumn aColumn)
    {
    	if(!(aColumn instanceof SimpleColumn))
    	{
    		return;
    	}
    	
    	int columnModelIndex = 0;
    	for(int i=0;i<getColumnCount();i++)
    	{
    		if(getColumn(i) instanceof ComplexColumn)
    		{
    			columnModelIndex+=((ComplexColumn)getColumn(i)).childs.size()-1;
    		}
    		else
    		{
    			columnModelIndex++;
    		}
    	}
    	aColumn.setModelIndex(columnModelIndex);
    	super.addColumn(aColumn);
    }
    
    public void addInstantColumn(TableColumn aColumn)
    {
    	super.addColumn(aColumn);
    }
    
    public void removeColumn(int colIndex)
    {
    	SimpleColumn colToRemove = getColumn(colIndex);
    	for(int i=colIndex+1;i<getColumnCount();i++)
    	{
    		getColumn(i).realModelIndex--; //TODO Complex column
        	getColumn(i).setModelIndex(getColumn(i).getModelIndex()-1);
    	}
    	super.removeColumn(colToRemove);
    }
    
    /**
     * Provides an Iterator to iterate over the
     * ColumnGroup list.
     * @return Iterator over ColumnGroups
     */    
    public Iterator columnGroupIterator()
    {
        return columnGroups.iterator();
    }
    
    /**
     * Returns a ColumnGroup specified by an index.
     * @param index index of ColumnGroup
     * @return ColumnGroup
     */    
    public ColumnGroup getColumnGroup(int index)
    {
        if(index >= 0 && index < columnGroups.size())
        {
            return columnGroups.get(index);
        }
        return null;
    }
    
    /**
     * Provides and iterator for accessing the ColumnGroups
     * associated with a column.
     * @param col Column
     * @return ColumnGroup iterator
     */    
    public Iterator getColumnGroups(SimpleColumn col)
    {
        if (columnGroups.isEmpty())
        {
        	return null;
        }
        for(int i=0;i<columnGroups.size();i++)
        {
        	ColumnGroup curGroup = columnGroups.get(i);
        	ArrayList<SimpleColumn> groups = curGroup.getColumnGroups(col,new ArrayList<SimpleColumn>());
        	if(groups!=null)
        	{
        		return groups.iterator();
        	}
        }
        return null;
    }
    
    @Override
    public SimpleColumn getColumn(int arg0)
    {
    	return (SimpleColumn)super.getColumn(arg0);
    }
    
    public void removeAllColumns()
    {
    	int colCount = getColumnCount();
    	for(int i=0;i<colCount;i++)
    	{
    		removeColumn(getColumn(0));
    	}
    }
    
    public void setColumnFixed(boolean value,int columnIndex)
    {
    	SimpleColumn colToFreeze = getColumn(columnIndex);
    	colToFreeze.setFreeze(value);
    	if(colToFreeze.parent!=null)
    	{
    		
    	}
    }
}


